﻿using EFCore.BulkExtensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using WalkingTec.Mvvm.Core;
using System.Diagnostics.CodeAnalysis;

namespace Digitalmes.Extensions
{
    /// <summary>
    /// WTM批量操作扩展
    /// </summary>
    public static class WtmBulkExtensions
    {
        /// <summary>
        /// 将IDataContext转换为DbContext
        /// </summary>
     
        public static DbContext ToDbContext(this IDataContext dataContext)
        {
            if (dataContext is DbContext dbContext)
            {
                return dbContext;
            }
            return new EmptyContext();  
        }

        /// <summary>
        /// 尝试将IDataContext转换为DbContext
        /// </summary>
        public static bool TryToDbContext(this IDataContext dataContext, out DbContext dbContext)
        {
            if (dataContext is DbContext context)
            {
                dbContext = context;
                return true;
            }

            dbContext = null;
            return false;
        }

        /// <summary>
        /// 批量插入
        /// </summary>
        public static async Task BulkInsertAsync<T>(this IDataContext dataContext,
            List<T> entities,
            BulkConfig bulkConfig = null,
            Action<decimal> progress = null,
            CancellationToken cancellationToken = default) where T : class
        {
            var dbContext = dataContext.ToDbContext();
            await dbContext.BulkInsertAsync(entities, bulkConfig);
        }

        /// <summary>
        /// 批量更新
        /// </summary>
        public static async Task BulkUpdateAsync<T>(this IDataContext dataContext,
            List<T> entities,
            BulkConfig bulkConfig = null,
            Action<decimal> progress = null,
            CancellationToken cancellationToken = default) where T : class
        {
            var dbContext = dataContext.ToDbContext();
            await dbContext.BulkUpdateAsync(entities, bulkConfig);
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        public static async Task BulkDeleteAsync<T>(this IDataContext dataContext,
            List<T> entities,
            BulkConfig bulkConfig = null,
            Action<decimal> progress = null,
            CancellationToken cancellationToken = default) where T : class
        {
            var dbContext = dataContext.ToDbContext();
            await dbContext.BulkDeleteAsync(entities, bulkConfig);
        }

        /// <summary>
        /// 批量插入或更新
        /// </summary>
        public static async Task BulkInsertOrUpdateAsync<T>(this IDataContext dataContext,
            List<T> entities,
            BulkConfig bulkConfig = null,
            Action<decimal> progress = null,
            CancellationToken cancellationToken = default) where T : class
        {
            var dbContext = dataContext.ToDbContext();
            await dbContext.BulkInsertOrUpdateAsync(entities, bulkConfig);
        }

        /// <summary>
        /// 批量读取
        /// </summary>
        public static async Task BulkReadAsync<T>(this IDataContext dataContext,
            List<T> entities,
            BulkConfig bulkConfig = null,
            Action<decimal> progress = null,
            CancellationToken cancellationToken = default) where T : class
        {
            var dbContext = dataContext.ToDbContext();
            await dbContext.BulkReadAsync(entities, bulkConfig, progress);
        }

        /// <summary>
        /// 批量截断表
        /// </summary>
        public static async Task TruncateAsync<T>(this IDataContext dataContext,
            CancellationToken cancellationToken = default) where T : class
        {
            var dbContext = dataContext.ToDbContext();
            await dbContext.TruncateAsync<T>();
        }
    }
}